define(function(require, exports, module) {

	"use strict";
	require("mui");
	require("jquery");
	var abpdata = require("abpinit");
	var abp = abpdata.abpstart;
	var template = require("template");
	var muiTips = require("muiTips"); //封装的弹框
	var globalInit = require("globalInit"); //共用js
	require('http://g.alicdn.com/ilw/ding/0.8.9/scripts/dingtalk.js');
	//返回清除缓存
	var dingTalkNav = require('dd_config');

	//调用页面权限控制接口
	var sourceType = abp.sourceType; //获取权限类型接口数据
	var authorityInit = require("operatAuthority"); //调用封装的权限方法
	var authorityData = sessionStorage.getItem('operatAuthorityData'); //获取权限数据
	var authoritySalesDet = authorityInit.getJurisdiction(authorityData, sourceType.SELL); //采购记录权限判断
	function getRightFun() {
		var popoverMore = $("header > a").attr("href");
		mui(popoverMore).popover('toggle');
	}
	if(authoritySalesDet.indexOf(3) === -1) {
		muiTips.alertTips(' ', '您没有操作销售单的权限，请联系管理员开通吧！', 2, 3, "default");
		$("header > a").remove();
		dingTalkNav.RightNavBar(false, "更多", "default");
	} else {
		$("header > a").show();
		dingTalkNav.RightNavBar(true, "更多", getRightFun);
	}
	if(authoritySalesDet.indexOf(2) === -1) {
		$('#creatSellOut').remove();
	} else {
		$('#creatSellOut').show();
	}

	var eHistory = require("eHistory");
	mui.init({
		swipeBack: true //启用右滑关闭功能
	});

	/*a链接跳转问题*/
	mui('#editForMore').on('tap', 'a', function() {
		document.location.href = this.href + "?dd_nav_bgcolor=FFFF943E";
	});
	//接收审批中心的数据
	var SourceId = sessionStorage.getItem('SourceId');
	var AuditId = sessionStorage.getItem('AuditId');
	var Audit_detailId = sessionStorage.getItem('Audit_detailId');
	var SourceType = sessionStorage.getItem('SourceType');
	//接收首页销售订单订单列表的数据
	var outPirchaseID = sessionStorage.getItem('outPirchaseID');
	if(sessionStorage.getItem('outPSourceType') && sessionStorage.getItem('outPSourceID')) {
		var outPSourceType = sessionStorage.getItem('outPSourceType');
		var outPSourceID = sessionStorage.getItem('outPSourceID');
	}

	//返回清除缓存
	function clearSessionDD() {
		sessionStorage.removeItem('sellId');
		sessionStorage.removeItem('sellData');
		sessionStorage.removeItem('strStatusId');
		sessionStorage.removeItem('goodsIndex');
		sessionStorage.removeItem('setParm');
		sessionStorage.removeItem('strStatusId');
		sessionStorage.removeItem('goodsUnit');
		sessionStorage.removeItem('flags');
		sessionStorage.removeItem('data-stockQuery');
		document.location.href = "../../src/salesOrder/index.html?dd_nav_bgcolor=FFFF943E";
	}
	//返回清除缓存→审批
	function clearSessionSPDD() {
		sessionStorage.removeItem('SourceId');
		sessionStorage.removeItem('AuditId');
		sessionStorage.removeItem('Audit_detailId');
		sessionStorage.removeItem('SourceType');
		document.location.href = "../Examination/index.html?dd_nav_bgcolor=FFFF943E";
	}

	//返回清除缓存
	function clearSession() {
		sessionStorage.removeItem('sellId');
		sessionStorage.removeItem('sellData');
		sessionStorage.removeItem('strStatusId');
		sessionStorage.removeItem('goodsIndex');
		sessionStorage.removeItem('setParm');
		sessionStorage.removeItem('strStatusId');
		sessionStorage.removeItem('goodsUnit');
		sessionStorage.removeItem('flags');
		sessionStorage.removeItem('data-stockQuery');
	}
	//返回清除缓存→审批
	function clearSessionSP() {
		sessionStorage.removeItem('SourceId');
		sessionStorage.removeItem('AuditId');
		sessionStorage.removeItem('Audit_detailId');
		sessionStorage.removeItem('SourceType');
	}

	function historyAudit() {
		eHistory.funAudit(AuditId, Audit_detailId, true, $('#notes').text());
	}

	function historyNoAudit() {
		eHistory.funAudit(AuditId, Audit_detailId, false, $('#notes').text());
	}
	mui('body').on('tap', '.mui-popover-action li>a', function() {
		var a = this,
			parent;
		//根据点击按钮，反推当前是哪个actionsheet
		for(parent = a.parentNode; parent != document.body; parent = parent.parentNode) {
			if(parent.classList.contains('mui-popover-action')) {
				break;
			}
		}
		//关闭actionsheet
		mui('#' + parent.id).popover('toggle');
		switch($(this).text()) {
			case '重置草稿':
				muiTips.confirmTips(' ', '您确定要重置吗！', 2, 1, resetDraft);
				break;
			case '通过':
				muiTips.alertTips(' ', '恭喜您已通过！', 1, 1, historyAudit);
				break;
			case '驳回':
				muiTips.alertTips(' ', '驳回成功！', 1, 1, historyNoAudit);
				break;
			case '编辑':
				break;
			case '删除':
				muiTips.confirmTips(' ', '您确定要删除吗！', 2, 1, deleteSell);
				break;
		}
	});

	var option = {};
	var dataModel = {};

	if(sessionStorage.getItem('SourceId') && sessionStorage.getItem('AuditId') && sessionStorage.getItem('Audit_detailId') && sessionStorage.getItem('SourceType')) {
		//审批中心入口

		dataModel = {
			"id": sessionStorage.getItem('SourceId')
		};
		//初始化页面显示

		$('header > a').attr('href', '#eForMore');

		//调用审批历史数据
		setTimeout(function() {
			eHistory.historyCon('sHistoryList', '#historyList', SourceType, SourceId);
		}, 1000);
		//返回清楚审批中心订单缓存
		//钉钉清除缓存
		dingTalkNav.LeftNavBar(clearSessionSPDD);
		//ad
		dingTalkNav.leftAndroid(clearSessionSPDD);
		mui('body').on('tap', 'header > button', function() {
			clearSessionSP();
		});
	} else {
		//首页入口
		dataModel = {
			"id": sessionStorage.getItem('sellId')
		};
		//		var stastusId = sessionStorage.getItem("strStatusId");

		if(sessionStorage.getItem('outPSourceType') && sessionStorage.getItem('outPSourceID')) {
			setTimeout(function() {
				eHistory.historyCon('sHistoryList', '#historyList', outPSourceType, outPSourceID);
			}, 1000);
		}
		//返回清除缓存
		mui('body').on('tap', 'header > button', function() {
			clearSession();
		});
		//钉钉清除缓存
		dingTalkNav.LeftNavBar(clearSessionDD);
		//ad
		dingTalkNav.leftAndroid(clearSessionDD);
		//生成销售出库单
		mui('body').on('tap', '#creatSellOut', function() {
			sessionStorage.removeItem('outPSourceType');
			sessionStorage.removeItem('outPSourceID');
			//			    sessionStorage.removeItem('strStatusId');
			sessionStorage.setItem('serialNo', $('#serialNo').text());
			sessionStorage.setItem('salDetID', $('#serialNo').attr("data-id"));
			document.location.href = '../newSell/newSellOutOrder.html?dd_nav_bgcolor=FFFF943E';
		});
		//查看已生成销售出库单
		mui('body').on('tap', '#querySellOut', function() {
			sessionStorage.removeItem('outPSourceType');
			sessionStorage.removeItem('outPSourceID');
			sessionStorage.removeItem('sellId');
			sessionStorage.removeItem('strStatusId');
			sessionStorage.setItem('inSalesID', $('#serialNo').attr("data-id"));
			document.location.href = '../outSales/index.html?dd_nav_bgcolor=FFFF943E';
		});

		//返回清除首页订单缓存
		if(sessionStorage.getItem('outPSourceType') && sessionStorage.getItem('outPSourceID')) {
			mui('body').on('tap', 'header > button', function() {
				sessionStorage.removeItem('outPSourceType');
				sessionStorage.removeItem('outPSourceID');
				sessionStorage.removeItem('sellData');
				sessionStorage.removeItem('strStatusId');
			});
		}
	}

	//	var option={};
	// 	var dataModel = {
	//			"id" : sessionStorage.getItem('sellId')
	//		}

	abp.services.app.sell.getModel(dataModel, option).
	done(function(data) {
		//      	console.log(data)
		//点击编辑存储该订单数据
		mui('body').on('tap', '#mui-update', function() {
			sessionStorage.setItem("sellData", JSON.stringify(data));
		});
		mui('body').on('tap', '#creatSellOut', function() {
			sessionStorage.setItem("sellData", JSON.stringify(data));
		});
		var isRef = data.isRef;
		var stastusId = data.statusId;
		if(isRef === true) {
			$("#querySellOut").show();
			$("#sellFixeddiv").show();
		} else {
			$("#querySellOut").hide();
		}
		////待入库/待出库
		if(stastusId == abp.statusId.Pending || stastusId == abp.statusId.Processing || stastusId == abp.statusId.Finish) {
			$('header > a').attr('href', '#forMore');
		} else {
			$('header > a').attr('href', '#editForMore');
		}
		if(stastusId == abp.statusId.Pending || stastusId == abp.statusId.Processing) {
			$("#creatSellOut").show();
			$("#sellFixeddiv").show();
		} else {
			$("#creatSellOut").hide();
			//			$("#sellFixeddiv").hide();
		}
		//		//完成、审核
		//		if(stastusId == abp.statusId.Finish) {
		//			$("#creatSellOut").hide();
		//			$("#sellFixeddiv").show();
		//		}

		//单据详情绑定
		$('#statusName').text(data.statusName);
		$('#statusName').attr("data-statusId", data.statusId);
		$('#serialNo').text(data.serialNo);
		$('#serialNo').attr("data-id", data.id);
		$('#serialDate').text(data.serialDate.substring(0, 10));
		$('#companyName').text(data.companyName);
		$('#busUserName').text(data.busUserName);
		var texttaxRateType = '';
		switch(data.taxRateType) {
			case 1:
				texttaxRateType = '不计税';
				break;
			case 2:
				texttaxRateType = '不含税价';
				break;
			case 3:
				texttaxRateType = '含税价';
				break;
		}
		$('#taxRateType').text(texttaxRateType);
		$('#totalAmount').text(data.totalAmount);
		//      	console.log(data.totalAmount)
		$('#operatorName').text(data.operatorName);
		$('#dataCreated').text(data.dataCreated.substring(0, 10));
		$('#notes').text(data.notes);
		//状态颜色
		var statusIdColor = require("colorOrder");
		statusIdColor("#statusName");
		//货品数据明细绑定
		var data1 = {};
		data1.list = data.sellDetail;
		var html = template('sellDetail', data1);
		//      	console.log(html)
		$("#detailSell").html(html);

		//审批历史数据绑

		if(data.success) {

		} else {
			//	            		console.log(data.error.message)

		}
	}).fail(function(data) {
		var muiTips = require("muiTips");
		muiTips.alertTips(' ', data.message, 2, 3, "default");
	});

	function backHref() {
		document.location.href = "../../src/salesOrder/index.html?dd_nav_bgcolor=FFFF943E";
	}
	//重置草稿
	function resetDraft() {
		var option = {};
		var dataModel = {
			"id": sessionStorage.getItem('sellId'),
			"serialNo": $('#serialNo').text()
		};
		abp.services.app.sell.resetDraft(dataModel, option).
		done(function(data) {
			var muiTips = require("muiTips");
			muiTips.alertTips(' ', "重置成功", 1, 3, backHref);

			if(data.success) {

			} else {
				//	            		console.log(data.error.message)		
			}
		}).fail(function(data) {
			var muiTips = require("muiTips");
			muiTips.alertTips(' ', data.message, 2, 3, "default");
		});

	}

	//删除单据
	function deleteSell() {
		var option = {};
		var dataModel = {
			"id": sessionStorage.getItem('sellId'),
			"serialNo": $('#serialNo').text()
		};
		//			console.log($('#serialNo').text())
		abp.services.app.sell.delete(dataModel, option).
		done(function(data) {
			var muiTips = require("muiTips");
			muiTips.alertTips(' ', "删除成功", 1, 3, backHref);

			if(data.success) {

			} else {
				//	            		console.log(data.error.message)

			}
		}).fail(function(data) {
			var muiTips = require("muiTips");
			muiTips.alertTips(' ', data.message, 2, 3, "default");
		});
	}

	globalInit.eHistoryInit("#historyList"); //审批历史数据提示
});